<?php
// +----------------------------------------------------------------------
// | xiongrui [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.xiongrui.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.xiongrui.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;

class Goodshops extends Admin {
  protected $Goodshops;
  public function _initialize() {
		parent::_initialize();
    $this->Goodshops = model('Goodshops');
	}

  public function index() {
		$map  = array('status' => array('gt', -1));
		$list = $this->Goodshops->where($map)->order('sort asc,id desc')->column('*', 'id');
		$this->assign('list', $list);
		$this->setMeta('商家管理');
		return $this->fetch();
	}

	public function add() {
    if (IS_POST) {
      	$result = $this->Goodshops->change();
      if($result){
        $this->success('添加成功！',  url('admin/Goodshops/index'));
      }else{
        $this->error('添加失败！');
      }
    } else {
			$data = array(
        'info'    => array('status' => 1,'sort'=>1),
				'keyList' => $this->Goodshops->addfield(),
			);
			$this->assign($data);
			$this->setMeta("添加商家");
			return $this->fetch('public/edit');
		}
  }

  public function edit($id) {
		if (!$id) {
			return $this->error("非法操作！");
		}
		if (IS_POST) {
			$result = $this->Goodshops->change();
			if ($result) {
				return $this->success("编辑成功！", url('admin/Goodshops/index'));
			} else {
				return $this->error("编辑失败！");
			}
		} else {
			$info = $this->Goodshops->where(array('id' => $id))->find();
			$data = array(
				'info'    => $info,
				'keyList' => $this->Goodshops->addfield(),
			);
			$this->assign($data);
			$this->setMeta('编辑商家');
			return $this->fetch('public/edit');
		}
	}

  public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$result = $this->Goodshops->where(array('id' => array('IN', $id)))->delete();
		if ($result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}
  /**
   * 更新商家线下二维码
   * @return [type] [description]
   */
  public function updataOfflinePayQR(){
    vendor("phpqrcode.phpqrcode");
    $list = $this->Goodshops->column('id');
    foreach ($list as $value) {
        $data = "http://www.xgw0851.com/index/cart/pay/shopid/".$value;
        $level = 'L';
        $size = 10;
        $path = "uploads/offlinepayqr/";
        $fileName = $path.''.$value.'.png';
        if(!file_exists($fileName )){
          \QRcode::png($data, $fileName, $level, $size);
        }
    }
    $this->success('更新成功!');
  }

}

 ?>
